
mod1 <- fixest::feols(avg_score_democ_support ~ pre_post | ID, data = estimation_data, cluster = ~ID)
mod2 <- fixest::feols(avg_score_democ_support ~ factor(A) | ID, data = estimation_data, cluster = ~ID)
mod3 <- fixest::feols(avg_score_trust_elect ~ pre_post | ID, data = estimation_data, cluster = ~ID)
mod4 <- fixest::feols(avg_score_trust_elect ~ factor(A) | ID, data = estimation_data, cluster = ~ID)

mod5 <- fixest::feols(avg_score_democ_support ~ pre_post | ID, data = estimation_data %>% filter(validated_election_outcome == "Elected"), cluster = ~ID)
mod6 <- fixest::feols(avg_score_democ_support ~ factor(A) | ID, data = estimation_data %>% filter(validated_election_outcome == "Elected"), cluster = ~ID)
mod7 <- fixest::feols(avg_score_trust_elect ~ pre_post | ID, data = estimation_data %>% filter(validated_election_outcome == "Elected"), cluster = ~ID)
mod8 <- fixest::feols(avg_score_trust_elect ~ factor(A) | ID, data = estimation_data %>% filter(validated_election_outcome == "Elected"), cluster = ~ID)

mods <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)

table_df <-
  modelsummary(mods,
               stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
               coef_map = c(
                 "pre_post" = "Post Election",
                 "factor(A)2" = "Wave 2 (ref. Wave 1)",
                 "factor(A)3" = "Wave 3 (ref. Wave 1)"
               ),
               gof_map = c("nobs", "r.squared"),
               output = "data.frame"
  ) %>%
  mutate(term = ifelse(statistic == "std.error", "", term)) %>%
  select(-c(part, statistic)) %>%
  mutate_all(as.character)

fe_row <- tibble::tibble(
  term = "Individual FEs",
  !!!setNames(rep("\\checkmark", ncol(table_df) - 1), names(table_df)[-1])
)

# Append the new row to the table
table_df <- bind_rows(table_df, fe_row)

latex_table <-
  table_df %>%
  kableExtra::kable("latex",
                    booktabs = T,
                    longtable = T,
                    escape = F,
                    cap = "\\label{tab:panel}Within-Subject Panel Analysis",
                    padding = 0,
                    col.names = linebreak(c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)"), c("l", rep("c", 8))),
                    align = c("l", rep("c", 10)),
                    linesep = "") %>%
  kableExtra::row_spec(6, hline_after = T) %>%
  kableExtra::kable_styling(
    font_size = 9,
    latex_options = c("scale_down",
                      "hold_position",
                      "striped")) %>%
  
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Supp. for Dem." = 2,
      "Trust in Elec." = 2,
      "Supp. for Dem." = 2,
      "Trust in Elec." = 2
    ),
    align = c("l", rep("c", 8))
  ) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "All Candidates" = 4,
      "Winning Candidates" = 4
    ),
    align = c("l", rep("c", 8))
  )

cat("\\renewcommand{\\arraystretch}{0.9}\n", file = "./outputs/tables/table_2.tex")
cat(latex_table, file = "./outputs/tables/table_2.tex", append = TRUE)

